﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;


namespace PaperCut
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();


          
            
        }

        //PathGeometry pg = new PathGeometry();
        //PathFigure pathFigure = new PathFigure();
        //PolyBezierSegment pbs = new PolyBezierSegment();
        List<System.Windows.Point> pbs = new List<System.Windows.Point>();

        BezierSegment tmpBS;
        bool startpointset = false;
        LineSegment line;
        void Touch_FrameReported(object sender, System.Windows.Input.TouchFrameEventArgs e)
        {
            if (!LayoutRoot.IsHitTestVisible) return;

            System.Windows.Input.TouchPointCollection tpc = e.GetTouchPoints(Application.Current.RootVisual);


            if (tpc.Count == 1)
            {
                TouchPoint tl = tpc[0];

                if (mode)
                {
                    if (tl.Action == TouchAction.Down)
                    {
                        lineFigure.Segments.Clear();
                        lineFigure.StartPoint = tl.Position;
                    }
                    if (tl.Action == TouchAction.Move)
                    {
                        if (lineFigure.Segments.Count == 0)
                        {
                            line = new LineSegment();
                            line.Point = tl.Position;
                            lineFigure.Segments.Add(line);

                        }
                        else line.Point = tl.Position;
                        if (pathFigure.Segments.Count > 0)
                        {
                            var pf = (QuadraticBezierSegment)pathFigure.Segments[0];
                            if (BezierIntersect(pathFigure.StartPoint, pf.Point1, pf.Point2, lineFigure.StartPoint, line.Point))
                            {
                                linePath.Stroke = new SolidColorBrush(Colors.Blue);
                            }
                            else linePath.Stroke = new SolidColorBrush(Colors.Red);
                        }
                    }
                    if (tl.Action == TouchAction.Up)
                    {
                        lineFigure.Segments.Clear();
                    }
                    //linePath.Opacity = 0.99;
                    //linePath.Opacity = 1;


                }
                else
                {


                    if (tl.Action == TouchAction.Down)
                    {

                        if (!startpointset)
                        {
                            pathFigure.StartPoint = tl.Position;
                            startpointset = true;
                            return;
                        }
                        if (pathFigure.Segments.Count == 0)
                        {

                            if (pbs.Count == 0)
                            {
                                pbs.Add(tl.Position);
                            }
                            else if (pbs.Count == 1)
                            {
                                pbs.Add(tl.Position);

                                QuadraticBezierSegment bs = new QuadraticBezierSegment();
                                bs.Point1 = pbs[0];
                                bs.Point2 = pbs[1];


                                pathFigure.Segments.Add(bs);
                                System.Windows.Point tmp = pbs[1];
                                pbs = new List<System.Windows.Point>();
                                pbs.Add(tmp);
                            }

                        }
                        else
                        {
                            pbs.Add(tl.Position);

                            QuadraticBezierSegment last = (QuadraticBezierSegment)pathFigure.Segments.Last();
                            System.Windows.Point tmp2 = last.Point2;
                            last.Point2 = Middle(last.Point1, last.Point2);

                            QuadraticBezierSegment bs = new QuadraticBezierSegment();
                            bs.Point1 = pbs[0];
                            bs.Point2 = pbs[1];

                            pathFigure.Segments.Add(bs);
                            System.Windows.Point tmp = pbs[1];
                            pbs = new List<System.Windows.Point>();
                            pbs.Add(tmp);
                        }



                        CutLine.Opacity = 0.99;
                        CutLine.Opacity = 1;



                    }

                }


            }


     
        }
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);

        }
        bool mode = false;
        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            mode = !mode;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            pathFigure.Segments.Clear();
            startpointset = false;
            pbs = new List<System.Windows.Point>();

        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {

            lineFigure.Segments.Clear();
            List<Point> pts = pathToPolygon(pathFigure, 5);
            //lineFigure = new PathFigure();
            lineFigure.StartPoint = pts[0];
            for (int i = 1; i < pts.Count; i++)
            {
                LineSegment ls = new LineSegment();
                ls.Point = pts[i];
                lineFigure.Segments.Add(ls);
            }
            linePath.Opacity = 0.99;
            linePath.Opacity = 1;
        }
    }
}